package com.teach.servlet.web2;

import java.io.IOException;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.teach.commons.RequestUtil;
import com.teach.dto.NewsSearchDTO;
import com.teach.entity.News;
import com.teach.service.NewsService;
import com.teach.service.impl.NewsServiceImpl;

@WebServlet("/web2/news-add.s")
public class NewsAddServlet extends HttpServlet {

	private NewsService service = new NewsServiceImpl();
	private Logger log = LoggerFactory.getLogger(getClass());
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	
		News news = RequestUtil.loadParams(request, News.class);
		log.debug("装载的请求参数：{}",JSON.toJSONString(news));
		
		String msg = "ok";
		try {
			service.add(news);
		}catch (Exception e) {
			msg = e.getMessage();
		}
		
		response.reset();
		response.setContentType("text/json;charset=UTF-8");
		response.getWriter().print(JSON.toJSONString(msg));
		response.flushBuffer();
	}
}
